package com.ruitu.mall_98_seller.ui.user;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.ruitu.arad.Arad;
import com.ruitu.arad.base.BaseActivity;
import com.ruitu.arad.util.MD5Util;
import com.ruitu.arad.util.ToastUtils;
import com.ruitu.mall_98_seller.R;
import com.ruitu.mall_98_seller.mvp.model.UserModel;
import com.ruitu.mall_98_seller.mvp.presenter.UserPresenter;
import com.ruitu.mall_98_seller.support.util.ViewUtil;
import com.ruitu.router_module.AppHolder;
import com.ruitu.router_module.bean.EventModel;
import com.ruitu.router_module.bean.User;

import cn.jpush.android.api.JPushInterface;

public class LoginActivity extends BaseActivity<UserPresenter.LoginP, UserModel.LoginM> {
    private TextView tv_login;
    private EditText et_user, et_pwd;

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_login;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        tv_login = findViewById(R.id.tv_login);
        et_user = findViewById(R.id.et_user);
        et_pwd = findViewById(R.id.et_pwd);

        setOnClickListener(tv_login);

        String uName = Arad.preferences.getString(USER_NAME_KEY);
        String pwd = Arad.preferences.getString(PASSWORD_KEY);
        if (!TextUtils.isEmpty(uName)) {
            et_user.setText(uName);
        }
        if (!TextUtils.isEmpty(pwd)) {
            et_pwd.setText(pwd);
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v == tv_login) {
            if (ViewUtil.isTvEmpty(et_user)) {
                ToastUtils.showShortSafe("请输入用户名");
                return;
            }
            if (ViewUtil.isTvEmpty(et_pwd)) {
                ToastUtils.showShortSafe("请输入密码");
                return;
            }

            String uName = ViewUtil.getTvTxt(et_user);
            String md5Pwd = MD5Util.md5String(ViewUtil.getTvTxt(et_pwd));
//            rxList.add(p.login(uName, md5Pwd));
            rxList.add(p.login(uName, md5Pwd));
            showProgressWithText(true, "登录中");
        }
    }

    public static final String USER_NAME_KEY = "user_name_key";
    public static final String PASSWORD_KEY = "password_key";

    @Override
    public void onReqComplete(int code, boolean isOk, Object data) {
        if (code == 1) {// 登录成功
            User user = (User) data;
            // 存储用户名
            Arad.preferences.putString(USER_NAME_KEY, ViewUtil.getTvTxt(et_user)).flush();
            Arad.preferences.putString(PASSWORD_KEY, ViewUtil.getTvTxt(et_pwd)).flush();
//            if (JPushInterface.isPushStopped(Arad.app)) {
//                JPushInterface.resumePush(Arad.app);
//                Logg.i("极光推送服务已停止...");
//            } else {
//                Logg.i("极光推送服务运行中...");
//            }
            String uId = user.getId();
            String uId9 = uId.substring(0, 9);
            int sequence = Integer.parseInt(uId9);
            AppHolder.instance().setSequence(sequence);
            JPushInterface.setAlias(Arad.app, AppHolder.instance().getSequence(), uId);// 极光别名
            Arad.bus.post(new EventModel(19090553));// 通知连接融云
            Arad.bus.post(new EventModel(19090651));// 获取各种数量

            ToastUtils.showShortSafe("欢迎您，九八商城企业用户");
            try {
//                ToastUtils.showShortSafe("欢迎您，" + user.getCompany().getName());
            } catch (Exception e) {
                e.printStackTrace();
            }
//            startActivity(HomeActivity.class);
            finish();
        }
    }
}
